//解析数组类
function ParseArray() {
  var isArray = function (obj) {
    if (!obj) {
      return;
    }
    var arrType = (typeof obj.push + "").toUpperCase();
    if (arrType == "UNDEFINED") {
      return false;
    } else {
      return true;
    }
  };

  var isObject = function (obj) {
    if (!isArray(obj)) {
      var objType = (typeof obj + "").toUpperCase();
      if (objType == "OBJECT") {
        return true;
      }
    }
    return false;
  };

  var isNumber = function (value) {
    var reg = /^[0-9]*$/;
    return reg.test(value);
  };
  //编写的内置方法=============================================================
  //【拓展】创建表的语句需要定义的字段
  var getCreateTableSql = function (props) {
    var sql = "";
    if (isNumber(props)) {
      sql = "`" + props + "` int(11) NOT NULL,\n";
    } else {
      sql = "`" + props + "` varchar(255) NOT NULL,\n";
    }
    return sql;
  };
  //【拓展】创建表的语句需要定义的字段
  var getInsertWord = function (word) {
    if (isNumber(word)) {
      return word;
    } else {
      return "'" + word + "'";
    }
  };
  var getTableSql = function (createSql, name) {
    var sql1 = "DROP TABLE IF EXISTS `" + name + "`;\n";
    var sql2 = "CREATE TABLE `" + name + "` (\n";
    var sql3 = "`id` int(11) NOT NULL AUTO_INCREMENT,\n";
    var sql4 = createSql;
    var sql5 = "`remark` varchar(255) DEFAULT NULL,\n";
    var sql6 = "PRIMARY KEY (`id`)\n";
    var sql7 = ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
    var sql = sql1 + sql2 + sql3 + sql4 + sql5 + sql6 + sql7;
    console.log(sql + "\n");
    return sql;
  };

  var indexArrNum = 0; //记录插入的数量
  var getInsertSql = function (allPropsArr, obj, tableName) {
    var insertSql = "";
    allPropsArr.forEach((item, index, arr) => {
      if (!isArray(obj[item])) {
        insertSql += getInsertWord(obj[item]) + ", ";
      } else {
        insertSql += indexArrNum + ", ";
        hasArray = true;
      }
    });
    var sql1 = insertSql;
    var sql2 =
      "INSERT INTO `" + tableName + "` VALUES (NULL, " + sql1 + "'暂无备注');";
    console.log(sql2);
    return sql2;
  };

  //内部变量
  var createSql = "";
  var allPropsArr = [];
  var okCreateTable = true;
  var upTableName = "";
  var hasArray = false;
  var isFirst = true;
  //执行初始化数据
  var init = function () {
    allPropsArr = [];
  };

  var dealObject = function (obj, tableName) {
    init();
    if (isObject(obj)) {
      for (var props in obj) {
        createSql += getCreateTableSql(props);
        allPropsArr.push(props);
      }
      //得到当前的建表语句
      if (okCreateTable) {
        console.log("#==========================================");
        console.log("# TableName : " + tableName);
        console.log("#==========================================");
        getTableSql(createSql, tableName);
        okCreateTable = false;
      }
      //得到当前的插入数据
      indexArrNum++;
      getInsertSql(allPropsArr, obj, tableName);
    }
  };

  /*
  this.start = function (obj, tableName) {
    dealObject(obj, tableName);
  };
  */

  this.start = function (dataArr, tableName) {
    if (isArray(dataArr)) {
      if (dataArr.length >= 1) {
        dataArr.forEach((item, index, arr) => {
          dealObject(item, tableName);
        });
      }
    }
    return hasArray; //返回是否创建过含有数组的表字段
  };
}

import data from "../data.mjs";

var parse = new ParseArray();

parse.start(data.goods[0].foods, "foods");
